package com.xxmw.transaction.common.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;

/**
 * 火币WebSocket消息解压缩工具类
 */
public class HuoBiGZipUtils {

	public static final int BUFFER = 1024;

	/**
	 * 解压缩数据
	 * 
	 * @param data
	 * @return
	 * @throws IOException
	 * @throws Exception
	 */
	public static byte[] decompress(byte[] data) {
		try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
			 ByteArrayOutputStream baos = new ByteArrayOutputStream();) {
			decompress(bais, baos);
			data = baos.toByteArray();
			baos.flush();
			return data;
		} catch (Exception e) {
			throw new RuntimeException("火币WebSocket消息解压缩异常", e);
		}
	}

	/**
	 * 解压缩gzip数据
	 * 
	 * @param is
	 * @param os
	 */
	public static void decompress(InputStream is, OutputStream os) {
		try (GZIPInputStream gis = new GZIPInputStream(is);) {
			int count;
			byte data[] = new byte[BUFFER];
			while ((count = gis.read(data, 0, BUFFER)) != -1) {
				os.write(data, 0, count);
			}
		} catch (Exception e) {
			throw new RuntimeException("火币WebSocket消息解压缩异常", e);
		}
	}

}
